package DBSystem;


import java.util.Iterator;
import java.util.ArrayList;
import OrdersSystem.*;

/**
 * A class that represents an mock database with which the
 * orders system will communicate.
 * 
 * @author iSolutions Orders Team
 *
 */
public class DataBase {
	
	// common variables for all the classes
	/**
	 * Represents the relation of the orders stored into the database.  
	 */
	private static ArrayList<Order> orders = new ArrayList<Order>();
	/**
	 * Represents the relation of the users stored into the database.  
	 */
	private static ArrayList<RegisteredUser> users = new ArrayList<RegisteredUser>();
	/**
	 * Represents the relation of the alerts stored into the database.  
	 */
	private static ArrayList<UserAlert> alerts = new ArrayList<UserAlert>();
	
	/**
	 * Auto-generated default constructor.  
	 */
	public DataBase() {}
	
	/*####################### accessors ##################################*/
	
	// Order
	/**
	 * A method, which retrieves an order from the orders relation based on orderID.
	 * 
	 * @param orderID the unique identifier for an order.
	 * 
	*/
	public Order retrieveOrderByOrderId(int orderID) {
		for(Order order : orders) {
	        if(order.getOrderID() == orderID){  //if the current order has this value, return it
	            return order; 
	        }
	    }
		return null;
	}
	
	/**
	 * A method, which retrieves all the orders from the orders relation
	 * made by a particular customer based on customerID
	 * 
	 * @param customerID the unique identifier for a customer.
	 * 
	*/
	public ArrayList<Order> retrieveOrderByCustomerID(int customerID) {
		ArrayList<Order> customerOrders = new ArrayList<Order>();
		for(Order order : orders) {
			if(order.getCustomerID() ==  customerID){  //if the current order has this value
	        	customerOrders.add(order);  // insert it into the list
	        }
	    }
		return customerOrders;		
	}
	
	/**
	 * A method, which retrieves all the orders stored in the relation orders.
	 * It is used in historyTracking by an PnG employee.
	 * 
	 * @return ArrayList<Order> a list of existing orders.
	*/
	public ArrayList<Order> retrieveAllOrders() { 
		return orders;
	}
	
	// Alert
	/**
	 * A method, which retrieves a specific alert from the relation alerts.
	 * 
	 * @param alertID the unique identifier for an alert.
	*/	
	public UserAlert retrieveUserAlertByAlertID(int alertID) {
		for(UserAlert alert : alerts) {
			if(alert.getAlertID() == alertID){  //if the current alert has this value, return it
	            return alert; 
	        }
	    }
		return null;
	}

	// Customer
	/**
	 * A method, which retrieves a specific RegisteredUser from the relation users.
	 * 
	 * @param userID the unique identifier for an user.
	*/
	public RegisteredUser retrieveUser(int userID) {
		for(RegisteredUser user : users) {
			 if(user.getUserID() == userID){  //if the current alert has this value, return it
	            return user; 
	        }
	    }
		return null;
	}		
	
	// get Contact details
	/**
	 * A method, which retrieves the contact details of 
	 * a particular user from the relation users.
	 * 
	 * @param userID the unique identifier for an user.
	*/
	public ContactDetails retrieveContactDetails(int userID) {
		for(RegisteredUser user : users) {
				if(user.getUserID() == userID) {
					return user.getAddress();
				}					
		}
		return null;
	}
	
	// other functions
	/**
	 * A method, which returns the id of the last stored Order.
	 * It is used in Order id generation.
	 * 
	 * @return max orderID.
	*/
	public int lastStoredOrderId() { 
		int max = 0;
		System.out.println("Number of orders: " + orders.size());
		for(Order order : orders) {
			if (order.getOrderID() > max) // if this element is bigger store it
				max = order.getOrderID();
		}
		return max;
	}
	
	/*################################## mutators #############################33*/
	
	// adding elements
	/**
	 * A method, which stores a new Order in relation orders.
	 * 
	 * @param newOrder the newly created Order.
	*/
	public void storeOrder(Order newOrder) {
		orders.add(newOrder);
	}	

	/**
	 * A method, which stores a new alert in relation alerts.
	 * It is used only in main, for testing purposes.
	 * Storing of new alerts into the database will be done by
	 * the Deliveries System
	 * 
	 * @param newUserAlert the newly created alert.
	*/

	public void storeUserAlert(UserAlert newUserAlert) {
		alerts.add(newUserAlert);
	}
	
	/**
	 * A method, which stores a new RegisteredUser in relation users.
	 * It is used only in main, for testing purposes.
	 * 
	 * @param newUser the newly created User.
	*/
	public void storeUser(RegisteredUser newUser) {
		users.add(newUser);
	}
	
	/**
	 * A method, which removes orders, which can not be supported by the system. 
	 * Returns true for successful deletion or false for unsuccessful.
	 * 
	 * @param newUser the newly created User.
	*/
	public boolean removeOrder(int orderID) {		
		for (Iterator<Order> iter = orders.iterator(); iter.hasNext(); ) {
			Order order = iter.next();  // get the object to which the iterator is pointing
			if (order.getOrderID() == orderID) {  // if it has this unique orderID, delete it from the list
				iter.remove();
				return true;
			}
		}
		return false;
	}	
	
	/**
	 * A method, which deletes the existing relations in the database.
	 * 
	*/
	public void dropDatabase() { // deletes the elements from the database tables
		orders.clear();
		alerts.clear();
		users.clear();
	}


}
	
	
